package com.feichuang.chineseWoodCarving.business.impl;

import java.lang.reflect.Type;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.better517na.commons.Response;
import com.better517na.commons.StatusCode;
import com.better517na.javaKeyIdGenerator.KeyIDFactory;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * . 所有business类的基类
 * 
 * @author chunfeng
 *
 */
@Component("baseBusiness")
public class BaseBusinessImpl {
    @Autowired
    protected KeyIDFactory keyIDFactory;

    /**
     * . 使用日期格式
     */
    private static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();

    /**
     * . 使用日期格式
     */
    // 过滤Class类型的对象
    private static Gson gson1 = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").addSerializationExclusionStrategy(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipClass(Class<?> arg0) {
            if (arg0 != null) {
                if ("class".equalsIgnoreCase(arg0.getSimpleName())) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean shouldSkipField(FieldAttributes arg0) {
            return false;
        }
    }).create();

    // /**
    // * . 基类中的日子组件对象
    // */
    // @Resource(name = "logBusiness")
    // private LogBusiness logBusiness;
    //
    // /**
    // * TODO 获取logBusiness.
    // *
    // * @return 返回logBusiness
    // */
    // public LogBusiness getLogBusiness() {
    // return logBusiness;
    // }
    //
    // /**
    // * TODO 设置logBusiness.
    // *
    // * @param logBusiness
    // * 赋值logBusiness
    // */
    // public void setLogBusiness(LogBusiness logBusiness) {
    // this.logBusiness = logBusiness;
    // }

    /**
     * 返回成功.
     * 
     * @param content
     *            返回内容
     * @return
     */
    /**
     * {@inheritDoc}.
     */
    public String toSuccess(Object content) {
        Response<Object> responseObject = new Response<Object>(StatusCode.SUCCESS, null, content);
        return gson.toJson(responseObject);
    }

    /**
     * 返回成功.
     * 
     * @return 1
     */
    public String toSuccess() {
        Response<Object> responseObject = new Response<Object>(StatusCode.SUCCESS, null, null);
        return gson.toJson(responseObject);
    }

    /**
     * 成功.
     * 
     * @param content
     *            内容
     * @param message
     *            返回信息
     * @return 1
     */
    public String toSuccess(Object content, String message) {
        Response<Object> responseObject = new Response<Object>(StatusCode.SUCCESS, message, content);
        return gson.toJson(responseObject);
    }

    /**
     * 失败.
     * 
     * @return 1
     */
    public String toFailure() {
        return gson.toJson(new Response<Object>(StatusCode.ERROR, null, null));
    }

    /**
     * 失败.
     * 
     * @param message
     *            失败消息
     * @return 1
     */
    public String toFailure(String message) {
        return gson.toJson(new Response<Object>(StatusCode.ERROR, message, null));
    }

    /**
     * 失败.
     * 
     * @param code
     *            失败状态码(状态码来自com.better517na.commons.StatusCode或者程序自定义状态码)
     * @param message
     *            失败消息
     * @return 1
     */
    public String toFailure(int code, String message) {
        return gson.toJson(new Response<Object>(code, message, null));
    }

    /**
     * 失败.
     * 
     * @param content
     *            .
     * @param code
     *            失败状态码(状态码来自com.better517na.commons.StatusCode或者程序自定义状态码)
     * @param <T>
     *            .
     * @param message
     *            失败消息
     * @return 1
     */
    public <T> String toFailure(int code, String message, T content) {
        return gson.toJson(new Response<Object>(code, message, content));
    }

    /**
     * 通用返回.
     * 
     * @param code
     *            状态码
     * @param message
     *            信息
     * @param content
     *            内容
     * @return 1
     */
    public String toJson(int code, String message, Object content) {
        return gson.toJson(new Response<Object>(code, message, content));
    }

    /**
     * TODO 服务端返回的JSON结果中，凡序列化，content使用时可以直接强转.
     * 
     * @param <T>
     *            模板类型
     * @param json
     *            json内容
     * @param cla
     *            Response中Content应该对应的对象类型
     * @return Response结果
     */
    @SuppressWarnings({"rawtypes", "unchecked" })
    public <T> Response<T> toResponseObject(String json, Class<T> cla) {
        if (json == null || "".equals(json)) {
            toFailure();
        }
        Response response = gson.fromJson(json, Response.class);
        if (response.getContent() != null) {
            Map<Object, Object> content = (Map<Object, Object>) response.getContent();
            String content2 = gson.toJson(content);
            Object contentObject = gson.fromJson(content2, cla);
            return new Response(response.getCode(), response.getMessage(), contentObject);
        } else {
            return response;
        }
    }

    /**
     * TODO 凡序列化.
     * 
     * @param json
     *            json内容
     * @param type
     *            type类型
     * @return 结果
     */
    public Response<Object> toObject(String json, Type type) {
        return gson.fromJson(json, type);
    }

    /**
     * .
     * 
     * @param json
     *            json
     * @param type
     *            type
     * @return t
     * @param <T>
     *            T
     */
    public <T> T toObjectT(String json, Type type) {
        return gson.fromJson(json, type);
    }

    /**
     * TODO 凡序列化.
     * 
     * @param <T>
     *            模板类型
     * @param json
     *            json对象
     * @param cla
     *            模板类
     * @return 结果
     */
    public <T> T toObject(String json, Class<T> cla) {
        return gson.fromJson(json, cla);
    }

    /**
     * .
     * 
     * @param json
     *            json
     * @param cla
     *            cla
     * @return t
     * @param <T>
     *            t
     */
    public <T> T toObjectFromCsharp(String json, Class<T> cla) {
        return gson1.fromJson(json, cla);
    }

    /**
     * TODO 与以前的没有状态码的系统兼容.
     * 
     * @param content
     *            内容
     * @return 结果
     */
    public String toJson(Object content) {
        if (content == null) {
            return "";
        }
        return gson.toJson(content);
    }
}
